﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jQueryDotNetLib.Controls
{
    [ToolboxData("<{0}:ProgressBar runat=server />")]
    public class ProgressBar : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Options
        {
            get
            {
                if (ViewState["Options"] == null) return "";
                return (string)ViewState["Options"];
            }

            set
            {
                ViewState["Options"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public double Value
        {
            get
            {
                if (ViewState["Value"] == null) return 0;
                return (double)ViewState["Value"];
            }
            set
            {
                ViewState["Value"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientChange
        {
            get
            {
                if (ViewState["OnClientChange"] == null) return "";
                return (string)ViewState["OnClientChange"];
            }

            set
            {
                ViewState["OnClientChange"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {

            //base.Render(writer);

            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<div id=" + this.ClientID + "></div>");

            sb.Append("<script type=\"text/javascript\">");
            sb.Append("jQuery(document).ready(function($) { var ctl = $(\"#" + this.ClientID + "\"); ctl.progressbar( { ");

            if (Options != "") sb.Append(Options + ",");

            bool needsComma = false;


            if (OnClientChange != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("change: function(event, ui) {" + OnClientChange + "}");
                needsComma = true;
            }
            if (Value > 0)
            {
                if (needsComma) sb.Append(",");
                sb.Append("value: " + Value + "");
                needsComma = true;
            }

            sb.Append("} ); ");

            sb.Append("});");
            sb.Append("</script>");

            writer.Write(sb.ToString());

        }

    }
}